<?php
	/**
	 * 系统配置类
	 * detail description
	 * @todo
	 * @author swingcoder <swingcoder@msn.com>
	 * @version 0.1
	 * @copyright Copyright (c) 2009, swingcoder.com
	 */

	class argument
	{
		var $database = null;
		var $tpl_type_cname = array( '', '节点页', '列表页', '内容页', '页头', '页脚', '首页', '其它' );
		var $tpl_type_ename = array( '', 'node', 'list', 'content', 'header', 'footer', 'home', 'other' );
		//模板类型
		var $TP_NODE = 1;
		var $TP_LIST = 2;
		var $TP_CONTENT = 3;
		var $TP_HEADER = 4;
		var $TP_FOOTER = 5;
		var $TP_HOME = 6;
		var $TP_OTHER = 7;
		var $content_module = array( '', 'news', 'photo', 'video', 'music', 'flash', 'soft', 'product' );
		var $content_module_cname = array( '', '新闻', '相片', '视频', '音乐', 'flash', '软件', '产品' );
		//内容模型
		var $CM_NEWS = 1;
		var $CM_PHOTO = 2;
		var $CM_VIDEO = 3;


		function argument()
		{
			$this->database = load( 'database' );
		}

		//根据key获取配置内容
		function value( $name )
		{
			$result = $this->database->unique( "select value from argument where ename = '" . $name . "'" );
			return isset( $result['value'] ) ? $result['value'] : -1;
		}

		//返回配置内容列表数据
		function all( $data )
		{
			$size = isset( $data['size'] ) && preg_match( '!^\d+$!', $data['size'] ) ? $data['size'] : 12;
			$page = isset( $data['page'] ) && preg_match( '!^\d+$!', $data['page'] ) ? $data['page'] : 1;
			$order = isset( $data['order'] ) ? $data['order'] : 'id asc';
			$type_id = isset( $data['type_id'] ) ? $data['type_id'] : '';
			$where = ' where 1=1 ';
			if ( $type_id != '' )
			{
				$where .= ' and type_id=' . $type_id;
			}
			$count = 'from argument' . $where;
			$list = 'select * ' . $count . ' order by ' . $order;
			$result = $this->database->split( $count, $list, $size, $page );

			return $result;
		}

		//获取具体配置的数据
		function get( $data )
		{
			$result = array();
			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) ) $result = $this->database->unique( 'select * from argument where id = ' . $data['id'] );
			return $result;
		}

		//修改具体配置的数据
		function set( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['id']['kind'] = '';
			$field['id']['type'] = 'num';
			$field['id']['min'] = '1';
			$field['id']['max'] = '8';
			$field['id']['info'] = '请传递ID';

			$field['cname']['kind'] = '';
			$field['cname']['type'] = 'text';
			$field['cname']['min'] = '1';
			$field['cname']['max'] = '32';
			$field['cname']['info'] = '请输入名称';

			$field['value']['kind'] = '';
			$field['value']['type'] = 'text';
			$field['value']['min'] = '1';
			$field['value']['max'] = '255';
			$field['value']['info'] = '请输入内容';

			$field['type_id']['kind'] = '';
			$field['type_id']['type'] = 'num';
			$field['type_id']['min'] = '1';
			$field['type_id']['max'] = '10';
			$field['type_id']['info'] = '请选择类型';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				$this->database->start();
				$this->database->set( 'argument', $data, array( 'id' => $data['id'] ) );

				if ( $this->database->end() )
				{
					$result['success'] = true;
					$result['data']['id'] = $data['id'];
				}
				else
				{
					$result['message'][] = '执行修改失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}

		//添加新的配置内容
		function add( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$validation = load( 'validation' );
			$field = array();

			$field['cname']['kind'] = '';
			$field['cname']['type'] = 'text';
			$field['cname']['min'] = '1';
			$field['cname']['max'] = '32';
			$field['cname']['info'] = '请输入名称';

			$field['value']['kind'] = '';
			$field['value']['type'] = 'text';
			$field['value']['min'] = '1';
			$field['value']['max'] = '255';
			$field['value']['info'] = '请输入内容';

			$field['ename']['kind'] = '';
			$field['ename']['type'] = 'text';
			$field['ename']['min'] = '1';
			$field['ename']['max'] = '32';
			$field['ename']['info'] = '请输入变量名';

			$field['type_id']['kind'] = '';
			$field['type_id']['type'] = 'num';
			$field['type_id']['min'] = '1';
			$field['type_id']['max'] = '255';
			$field['type_id']['info'] = '请选择类型';

			$check = $validation->validate( $field, $data );

			if ( count( $check ) == 0 )
			{
				$this->database->start();
				$id = $this->database->add( 'argument', $data );

				if ( preg_match( '!^\d+$!', $id ) && $this->database->end() )
				{
					$result['success'] = true;
					$result['data']['id'] = $id;
				}
				else
				{
					$result['message'][] = '执行创建失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}

		//删除具体配置的数据
		function del( $data )
		{
			$result = array();
			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) )
			{
				$result = $this->database->command( 'delete from argument where id = ' . $data['id'] );
			}
			return $result;
		}

	}
?>